package services

import (
	"GOPAAS/internal/app/model"
	"GOPAAS/internal/utils"
	"strconv"
	"strings"
	"time"
)

type CrmLeadService struct {
	model.ResCompany
}

func GetCrmLeadService() *CrmLeadService {
	return &CrmLeadService{}
}

// AddLeadInfo 联系我们
func (c CrmLeadService) AddLeadInfo(name string, contact_name string, contact_email string, phone string, description string) error {
	db = getDB()
	var lead model.CrmLead
	_, week := time.Now().ISOWeek()
	lead = model.CrmLead{
		Name:         name,
		ContactEmail: contact_email,
		ContactName:  contact_name,
		Phone:        phone,
		Description:  description,
		Type:         "opportunity",
		CountryId:    48,
		Year:         time.Now().Format("2006"),
		Month:        strings.TrimPrefix(time.Now().Format("01"), "0"),
		Hour:         strings.TrimPrefix(time.Now().Format("15"), "0"),
		Week:         strconv.Itoa(week),
		WeekStart:    strconv.Itoa(utils.GetWeekIndex(time.Now().Weekday().String())),
		Quarter:      strconv.Itoa(utils.GetQuarter(time.Now())),
		AccessToken:  strings.ToLower(utils.GetUuid(32, 1)),
	}
	err := db.Select("Name", "ContactEmail", "ContactName", "Phone", "Description", "Type", "CountryId", "Year", "Month", "Hour", "Week", "WeekStart", "Quarter", "AccessToken").Create(&lead)
	return err.Error
}

type Clients struct {
	Name        string //名称
	Description string //描述
	Level       string //评级
	Image       string //图片
}

// MyClients 首页我们的客户
func MyClients(limit int) (res []Clients) {
	db = getDB()
	var clients []model.CrmLead
	db.Where("stage_id=?", 5).Order("create_date desc").Select("id", "Name", "Description", "IsHot").Limit(limit).Find(&clients)
	for _, v := range clients {
		var level = "满意"
		if v.IsHot {
			level = "非常满意"
		}
		res = append(res, Clients{
			Name:        v.Name,
			Description: v.Description,
			Level:       level,
			Image:       GetSimpleImage("crm.lead", v.Id, "image"),
		})
	}
	return
}
